﻿namespace EasyGo.Web.Controllers
{
    using System.Web.Mvc;
    using EasyGo.Domain.Model;
    using EasyGo.Web.Authentication;
    using EasyGo.Web.Models;
    using Resources;

    public class AccountController : Controller
    {
        private readonly IAuthenticationService _auth;

        public AccountController(IAuthenticationService service)
        {
            _auth = service;
        }

        public ActionResult Login ()
        {
            return this.View(new Login());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(Login model)
        {
            if(ModelState.IsValid)
            {
                UserInfo info = _auth.ValidateUser(model.Email, model.Password);
                if (info != null)
                {
                    _auth.Authenticate(info, model.RememberMe);
                    return this.RedirectToAction("Index", "Home");
                }
                ModelState.AddModelError("", Loc.IncorrectLogin);
            }
            return this.View(model);
        }

        public ActionResult LogOut()
        {
            _auth.SignOut();
            return this.RedirectToAction("Index", "Home");
        }
    }
}
